//
//  MainViewController.m
//  001LiycSinaWeiBo
//
//  Created by 李伟国 on 15/9/8.
//  Copyright (c) 2015年 李伟国. All rights reserved.
//

#import "MainViewController.h"
#import "HomePageViewController.h"
#import "FansListViewController.h"
#import "MineWeiboViewController.h"
#import "AttentionListViewController.h"
#import "MineViewController.h"
#import "RDVTabBarItem.h"
#import "LoginViewController.h"
@interface MainViewController ()<RDVTabBarControllerDelegate>

@end

@implementation MainViewController

- (void)dealloc {

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
    
    
    
    [self setViewControllers];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucess:) name:LOGIN_SUCESS object:nil];
    
}
#pragma mark --RDVTabBarControllerDelegate
- (BOOL)tabBarController:(RDVTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

//    NSLog(@"%s %@",__func__,viewController);
    
    NSArray *vcs = tabBarController.viewControllers;
    NSInteger index = [vcs indexOfObject:viewController];
    
    NSLog(@"%ld",index);
    
    
//    先获取token，如果token为nil，必须登陆，token不为空，过期，必须登陆，token不为空，且不过期，无需再次登陆
    
    
    
    
    return YES;
}


- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    
    [self login];
    
}


- (void)login {

    NSString *access_token = [[NSUserDefaults standardUserDefaults] objectForKey:ACCESS_TOKEN];
    
    if (access_token == nil || [Help isNeedToRefreshTheToken]) {
        
        //        登
            
            LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
            UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
            
            [self presentViewController:loginNav animated:YES completion:nil];
        
        }
}


#pragma mark --登陆成功
- (void)loginSucess:(NSNotification *)noti {
//登陆成功接收到数据
    NSString *content = noti.object;
//    http://hi.baidu.com/jt_one#access_token=2.008kmTQDjoxjvDbce659177bPQkUGD&remind_in=124467&expires_in=124467&uid=2991918775
    
//    2.008kmTQDjoxjvDbce659177bPQkUGD
    
    NSString *access_token = [Help getContentFor:@"access_token=" andPath:content];
    NSString *expires_in = [Help getContentFor:@"expires_in=" andPath:content];
    NSString *uid = [Help getContentFor:@"uid=" andPath:content];
    
//    保存token和uid
    [[NSUserDefaults standardUserDefaults] setObject:access_token forKey:ACCESS_TOKEN];
    [[NSUserDefaults standardUserDefaults] setObject:uid forKey:UID];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    if (expires_in != nil) {
        NSTimeInterval time = [expires_in longLongValue] - 3600;
        NSDate *expiresDate = [NSDate dateWithTimeIntervalSinceNow:time];
        [[NSUserDefaults standardUserDefaults] setObject:expiresDate forKey:EXPIRES_DATE];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
}



//配置控制器
- (void)setViewControllers {

    HomePageViewController *homePageVC = [[HomePageViewController alloc] initWithNibName:@"HomePageViewController" bundle:nil];
    UINavigationController *homePageNav = [[UINavigationController alloc] initWithRootViewController:homePageVC];
    
    FansListViewController *fansListVC = [[FansListViewController alloc] initWithNibName:@"FansListViewController" bundle:nil];
    UINavigationController *fansListNav = [[UINavigationController alloc] initWithRootViewController:fansListVC];
    
    MineWeiboViewController *mineWeiboVC = [[MineWeiboViewController alloc] initWithNibName:@"MineWeiboViewController" bundle:nil];
    UINavigationController *mineWeiboNav = [[UINavigationController alloc] initWithRootViewController:mineWeiboVC];
    
    AttentionListViewController *attentionListVC = [[AttentionListViewController alloc] initWithNibName:@"AttentionListViewController" bundle:nil];
    UINavigationController *attentionListNav = [[UINavigationController alloc] initWithRootViewController:attentionListVC];
    
    MineViewController *mineVC = [[MineViewController alloc] initWithNibName:@"MineViewController" bundle:nil];
    UINavigationController *mineNav = [[UINavigationController alloc] initWithRootViewController:mineVC];
    
    NSArray *viewControllers = @[homePageNav, fansListNav,  mineWeiboNav, attentionListNav, mineNav];
    
    self.viewControllers = viewControllers;
    
    [self customizeTabBarForController:self];
    
}

- (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {
    UIImage *finishedImage = [UIImage imageNamed:@"tabbar_selected_background"];
    UIImage *unfinishedImage = [UIImage imageNamed:@"tabbar_normal_background"];
    NSArray *tabBarItemImages = @[@"tabbar_limitfree", @"tabbar_reduceprice", @"tabbar_appfree",@"tabbar_subject",@"tabbar_rank"];
    
    NSArray *titles = @[@"主页",@"粉丝列表",@"我的微博",@"关注列表",@"我的"];
    
    NSInteger index = 0;
    
    NSDictionary *textAttributes_normal = nil;
    NSDictionary *textAttributes_selected = nil;
    
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        textAttributes_normal = @{
                                  NSFontAttributeName: [UIFont systemFontOfSize:12],
                                  NSForegroundColorAttributeName: [UIColor colorWithRed:65/255.0 green:65/255.0 blue:65/255.0 alpha:1.0],
                                  };
        textAttributes_selected = @{
                                    NSFontAttributeName: [UIFont systemFontOfSize:12],
                                    NSForegroundColorAttributeName: [UIColor colorWithRed:14/255.0 green:154/255.0 blue:255/255.0 alpha:1.0],
                                    };
    }
    
    
    for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {
        
        
        item.unselectedTitleAttributes = textAttributes_normal;
        item.selectedTitleAttributes = textAttributes_selected;
        
        
        [item setBackgroundSelectedImage:finishedImage withUnselectedImage:unfinishedImage];
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_press",
                                                      [tabBarItemImages objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@",
                                                        [tabBarItemImages objectAtIndex:index]]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        
        item.title = titles[index];
        
        index++;
        
        
        
    }
}

@end



